var BookingAccount = function() {
    // Util Class
    this.mUtil = null;

    // UI Select 데이터
    this.HTMLDom = null;

    // 라디오 버튼 선택 인덱스 값
    this.mRadioSelectIndex = null;
};

BookingAccount.prototype = {
    constructor : BookingAccount,
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // InterlockBooking class 에 연동요청
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 실시간 예약 부킹 상세 데이터 가져오는 연동
     */
    getRsvBookingData : function($reserv_id) {
        var thisClass = this;
        var interlockBooking = new InterlockBooking();

        $.mobile.showPageLoadingMsg();

        console.log($reserv_id);

        interlockBooking.getTimeBookingDetailData($reserv_id, function($data) {
            thisClass.reserv_data = $data;
            thisClass.setBookingDetailView(thisClass.reserv_data);
            $.mobile.hidePageLoadingMsg();
        }, function(e) {
            alert("fail :: " + JSON.stringify(e));
            $.mobile.hidePageLoadingMsg();
        });
    },
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Method
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    setBookingDetailView : function($data) {
        var data = $data;
        var thisClass = this;

        var title = this._HTMLDom.top_article.title_layer;
        var hole = this._HTMLDom.top_article.hole;
        var date = this._HTMLDom.top_article.date;

        var price_old = this._HTMLDom.top_article.price_old;
        var arrow = this._HTMLDom.top_article.arrow;
        var greenfee = this._HTMLDom.top_article.greenfee;
        var price = this._HTMLDom.top_article.price;

        var price_pay = this._HTMLDom.top_article.price_pay;

        var i = 0;
        var dateStr = "" + data.reserv_date;
        var timeStr = "" + data.reserv_time;
        var tempStr = "";

        title.empty();

        // 클럽 이름
        tempStr = data.club_name;

        while (i < data.course_name.length) {
            if (data.course_name[i] != undefined) {
                tempStr += " " + data.course_name[i];
            }
            i++;
        }
        title.html(tempStr);

        // 홀
        hole.html(" " + data.hole + "H");
        title.append(hole);

        // 날짜, 시간
        date.html(dateStr.substr(0, 4) + "." + dateStr.substr(4, 2) + "." + dateStr.substr(6, 2) + "(" + data.reserv_day + ") " + timeStr.substr(0, 2) + ":" + timeStr.substr(2, 2));
        title.append(date);
        console.log(data);
        if (data.price > data.greenfee_pay && data.greenfee_pay != undefined) {
            // 할인가가 정가보다 저렴할때
            price.hide();
            // 정가
            price_old.find('span').html(this.mUtil.getMoneyTypeNum(data.price) + " 원");

            // 할인가
            greenfee.find('mark').html(this.mUtil.getMoneyTypeNum(data.greenfee_pay));
        } else {
            // 할인가와 정가가 같거나 할인가가 클때
            price_old.hide();
            arrow.hide();
            greenfee.hide();

            price.find('span > mark').html(this.mUtil.getMoneyTypeNum(data.price));
        }

        // 가승인 결재 금액
        price_pay.html("￦ " + this.mUtil.getMoneyTypeNum(data.price_pay));
    },
    openSignPage : function() {
        console.log("openSignPage");
        if (this.mRadioSelectIndex == null) {
            alert("결재 방식을 선택해야 합니다.");
            return;
        }

        if (navigator.userAgent.match(/(iPhone|iPod|iPad)/i)) {
            // IOS
            // TODO
        } else if (navigator.userAgent.match(/(Android)/i) != null) {
            // Android
            // TODO
            alert("openSignPage");
            console.log(this.reserv_data.reserv_id);
            var url = "file:///android_asset/www/inc/nicepay_billkey.html";
            // var url = "http://192.168.0.82/mobile/inc/nicepay_billkey.html";

            window.localStorage.setItem(document.globalObj.STORAGE_KEY.billing_reserv_type, "R");
            window.localStorage.setItem(document.globalObj.STORAGE_KEY.billing_reserv_id, this.reserv_data.reserv_id);
            window.localStorage.setItem(document.globalObj.STORAGE_KEY.billing_user_id, document.globalObj.LOGIN_DATA.userId);
            
            if (this.mRadioSelectIndex == 1) {
                window.localStorage.setItem(document.globalObj.STORAGE_KEY.billing_method, "CELLPHONE");
            } else if (this.mRadioSelectIndex == 2) {
                window.localStorage.setItem(document.globalObj.STORAGE_KEY.billing_method, "CARD");
            }
            
            // TODO :: TEST
            window.plugins.childBrowser.onLocationChange = function(loc) {
                console.log("location change :: " + loc);
                return;
                var failurl = "https://web.nicepay.co.kr/smart/card/payResultFail.jsp";
                if (loc.indexOf(failurl) == 0) {
                    window.plugins.childBrowser.close();
                    alert("결제가 실패하였습니다.");
                }

            };
            window.plugins.childBrowser.showWebPage(url, {
                showLocationBar : false
            });
        }
    },

    /**
     * 필요한 테이터 셋팅
     */
    setData : function($data) {
        this.reserv_id = $data.bookingview.reserv_id;
        this.reservation = $data.bookingview.reservation;
        this.area = $data.bookingview.area;
    },
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Initialize
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    onPageShow : function() {
        console.log("BookingAccount onPageBeforeShow!");
        var thisClass = this;

        this.mUtil = document.globalObj.util;

        // 부킹 데이터 셋팅 및 초기화
        this.setData(document.globalObj.BOOKING_DATA);

        var article_layer = $('#booking_acc_detail_contents').find("article:first");
        var contents_layer = article_layer.find("div:first");
        var radio_broupt_layer = $('#booking_acc_detail_contents > div');

        this._HTMLDom = {
            // 상단 Article
            "top_article" : {
                "title_layer" : article_layer.find("h5"), // 타이틀
                "hole" : article_layer.find("h5 > em"), // 홀
                "date" : article_layer.find("h5 > mark"), // 날짜
                "price_old" : contents_layer.find("div:first"), // 할인 이전가
                "arrow" : contents_layer.find('div:eq(1)'), // 화살표
                "greenfee" : contents_layer.find("div:eq(2)"), // 할인가
                "price" : contents_layer.find('div:last'), // 정상가
                "price_pay" : article_layer.find("div:last > mark"),        // 가승인 결재 금액
            },
            "radio_group" : radio_broupt_layer
        };

        // 버튼 이벤트
        $('#btn_back').click(function() {
            $.mobile.changePage("booking_view.html");
        });
        // 주의사항
        $('#go_booking_help').click(function() {
            $.mobile.changePage("booking_help.html");
        });

        // 라디오 버튼 그룹
        this._HTMLDom.radio_group.change(function($event) {
            thisClass.mRadioSelectIndex = $(':radio:checked').val();
        });

        // 다음 버튼
        $('#btn_next').click(function() {
            console.log(thisClass.mRadioSelectIndex);
            // $.mobile.changePage("booking_account_01.html");
            // TODO :: TEST Link - 임시로 결제 완료 페이지로 이동
            var data = {
                // 예약 번호
                reserv_id : thisClass.reserv_id,
                // 예약 종류
                reservation : thisClass.reservation,
                // 지역
                area : thisClass.area,
                // 예약 타입
                reserv_type : "G"
            };
            document.globalObj.BOOKING_DATA.account_ok = data;
            // $.mobile.changePage("booking_account_ok.html");
            thisClass.openSignPage();
        });

        $('section div[data-role="fieldcontain"]').change(function($index) {
            thisClass.mRadioSelectIndex = $(this).find(':checked').val();
        });
        // 부킹 상세 데이터 가져오기
        this.getRsvBookingData(this.reserv_id);
    }
};

